#!/bin/bash
set -o errexit # Set the shell option to exit on error

DIRNAME=`dirname $0`
source ${DIRNAME}/action_functions.bash
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'


function createConfig(){
mkdir -p /etc/docker/
cat>/etc/docker/daemon.json<<\EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": [
    "https://2ph0ua9a.mirror.aliyuncs.com",
    "https://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ],
  "data-root":"/var/lib/docker",
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "50m",
    "max-file": "6"
  },
  "userland-proxy": true,
  "_comment": {"bip": "172.17.0.0/16"},
  "live-restore": true,
  "ipv6": false
}
EOF
}

function deal(){

  if [ ! -f /etc/docker/daemon.json ] ;then
     createConfig
  fi
  if [ ! -f /etc/yum.repos.d/docker-ce.repo ] ;then
    curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - > /dev/null 2>&1
    add-apt-repository -y "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" > /dev/null 2>&1
    DOCKER_VERSION="5:20.10.23~3-0~ubuntu-$(lsb_release -cs)"
    CONTAINERD_VERSION="1.6.19-1"
    apt-get install containerd.io=${CONTAINERD_VERSION} -y > /dev/null 2>&1
    containerd config default > /etc/containerd/config.toml
    sed -i 's@registry.k8s.io@registry.aliyuncs.com/google_containers@g' /etc/containerd/config.toml
    sed -i s@'SystemdCgroup = false'@'SystemdCgroup = true'@g /etc/containerd/config.toml
    sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors/a\        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]' /etc/containerd/config.toml
    sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"/a\        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]' /etc/containerd/config.toml
    sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"/a\        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."ghcr.io"]' /etc/containerd/config.toml
    sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."ghcr.io"/a\          endpoint = ["https://registry-1.docker.io", "https://ghcr.io"]' /etc/containerd/config.toml
    sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"/a\          endpoint = ["https://gcr.azk8s.cn", "https://gcr.io"]' /etc/containerd/config.toml
    sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"/a\          endpoint = ["https://fogjl973.mirror.aliyuncs.com", "https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn","https://registry-1.docker.io"]' /etc/containerd/config.toml
    apt-get install docker-ce=${DOCKER_VERSION} docker-ce-cli=${DOCKER_VERSION} docker-ce-rootless-extras=${DOCKER_VERSION} docker-compose-plugin docker-buildx-plugin docker-scan-plugin -y > /dev/null 2>&1
    systemctl disable --now docker.socket docker.service > /dev/null 2>&1
  fi
	
}

function main(){
  action $0 deal
}

main $*
